$(function () {
    //省略号
    $('.add_info').each(function () {
        var sl = $(this).text();
        if (sl.length > 20) {
            sl = sl.substring(0, 14);
            $(this).text(sl + "...");
        }

    });


    //获取地址个数，调节样式
    var cnum = $(".choose_address_info").length;
    if (cnum > 4) {
        $(".clear").css("height", "260px");
    } else if (cnum === 0) {
        $(".clear").css("height", "10px");
    } else {
        $(".clear").css("height", "140px");
    }
    //计算应付金额
    var totalmoney = 0.00;
    $(".commo_each_total").each(function () {
        var each = parseFloat($(this).text());
        totalmoney = parseFloat(totalmoney) + each;
    });
     //弹出窗口
    $("#immediately_buy").click(function () {

        $("#BgDiv").css({display: "block", height: $(document).height(), width: $(document).width(), position: "absolute", top: "0", background: "#ecebeb", opacity: "0.6"});
        $(".prompt").show();
        document.documentElement.scrollTop = 60;
        $(".grade_line").css("margin-top", "-240px");
    });
   
    $(".prompt .buy").click(function () {
        var str = "["
        var aid = $(".default_address").find(".hiddenAddressId").val();
        var name = $(".default_address").find(".hiddenName").val();
        var mobile = $(".default_address").find(".hiddenMobile").val();
        $(".order_list_info_tr").each(function () {
            var price = $(this).find(".commo_price").text();
            var number = $(this).find(".commo_num").val();
            var total = $(this).find(".commo_each_total").text();
            var cartid = $(this).find(".hiddenCartId").val();
            var shopid = $(this).find(".hiddenShopId").val();
            var comid = $(this).find(".hiddenCommodityId").val();
            str += "{'address':'" + aid + "','name':'" + name + "','mobile':'" + mobile + "','price':'" + price + "','number':'" + number
                    + "','totalMoney':'" + total + "','shopId':'" + shopid + "','commodityId':'" + comid + "','cartId':'" + cartid + "'";
            str += "},";
        });
        str = str.substring(0, str.length - 1);
        str += "]";
        $.ajax({
            type: "post",
            url: "/addOrderByCart.do",
            timeout: 1000,
            data: {str: str},
            dataType: "text",
            cache: false,
            success: function (data) {
                if (data === "success") {
                    alert("保存订单成功!");
                    location.href = "/traning.do";
                } else {
                    alert("生成订单失败，请重新下单!");
                    $("#BgDiv").css("display", "none");
                    $(".prompt").css("display", "none");
                    $(".grade_line").css("margin-top", "85px");
                }
            }
        });
    });
    $(".cancle").click(function () {
        $("#BgDiv").css("display", "none");
        $(".prompt").css("display", "none");
        $(".grade_line").css("margin-top", "85px");
    });
    //减号
    $(".fa-minus-square-o").click(function () {
        var num = $(this).parent("td").children("input").val();
        var anum = 1;
        var tnum = 0.00;
        if (parseInt(num) > 1) {
            anum = parseInt(num) - 1;
            $(this).parent("td").children("input").val(anum);
        } else {
            anum = 1;
            $(this).parent("td").children("input").val(1);
        }
        var price = $(this).parent("td").prev("td").children("label").text();
        var p = parseFloat(price);
        var total = parseInt(anum) * p;
        $(this).parent("td").next("td").next("td").children("label").text(total.toFixed(2));
        $(".commo_each_total").each(function () {
            var each = parseFloat($(this).text());
            tnum = parseFloat(tnum) + parseFloat(each);
        });
        $(".all_commo_price").text(tnum.toFixed(2));
    });
    //加号
    $(".fa-plus-square-o").click(function () {
        var num = $(this).parent("td").children("input").val();
        var anum = 1;
        var tnum = 0.00;
        if (parseInt(num) >= 10) {
            anum = 10;
            $(this).parent("td").children("input").val(10);
        } else {
            anum = parseInt(num) + 1;
            $(this).parent("td").children("input").val(anum);
        }
        var price = $(this).parent("td").prev("td").children("label").text();
        var p = parseFloat(price);
        var total = parseInt(anum) * p;
        $(this).parent("td").next("td").next("td").children("label").text(total.toFixed(2));
        $(".commo_each_total").each(function () {
            var each = parseFloat($(this).text());
            tnum = parseFloat(tnum) + parseFloat(each);
        });
        $(".all_commo_price").text(tnum.toFixed(2));
    });
    $(".commo_num").keyup(function () {
        var num = $(this).val();
        var tnum = 0.00;
        if (parseInt(num) > 10) {
            alert("不能超过10");
            num = 10;
            $(this).val(10);
        }
        if (parseInt(num) === 0 || parseInt(num) < 0) {
            alert("不能低于1");
            num = 1;
            $(this).val(1);
        }
        if (num === undefined || num === "") {
            alert("不能低于1");
            num = 1;
            $(this).val(1);
        }
        var price = $(this).parent("td").prev("td").children("label").text();
        var p = parseFloat(price);
        var total = parseInt(num) * p;
        $(this).parent("td").next("td").next("td").children("label").text(total.toFixed(2));
        $(".commo_each_total").each(function () {
            var each = parseFloat($(this).text());
            tnum = parseFloat(tnum) + parseFloat(each);
        });
        $(".all_commo_price").text(tnum.toFixed(2));
    });
    $(".all_commo_price").text(totalmoney.toFixed(2));
    $(".choose_address_info").click(function () {
        $(".choose_address_info").find(".righttop").each(function () {
            $(this).attr("src", "/img/user/right_top_no.png");
        });
        $(".choose_address_info").find(".redsan").each(function () {
            $(this).attr("src", "/img/user/red_san_no.png");
        });
        $(".choose_address_info").find(".duigou").each(function () {
            $(this).hide();
        });
        $(".choose_address_info").find(".add_change").each(function () {
            $(this).hide();
        });
        $(".choose_address_info").find(".check_addname").each(function () {
            $(this).removeClass("add_name").addClass("add_name_other");
        });
        $(this).find(".righttop").attr("src", "/img/user/right_top.png");
        $(this).find(".redsan").attr("src", "/img/user/red_san.png");
        $(this).find(".duigou").show();
        $(this).find(".add_change").show();
        $(this).find(".check_addname").removeClass("add_name_other").addClass("add_name");
    });
});


